var allPro = null;
var allBase = null;
var voltage_type = null;
var opinion_type = null;
var where_type = null;
$(function () {
    $.when( getdatadictionary("voltage_type"),
        getdatadictionary("opinion_type"),
        getdatadictionary("where_type"),
        $.get(baseURL + 'bss/dpoint/select?dpGuid='+ GetUrlParam("dpGuid")+'&pGuid='+GetUrlParam("pGuid"), function (r) {
            allPro = r.returnlist;
        }),
        $.get(baseURL + 'bss/apbase/select?ourid=isok', function (r) {
            allBase = r.returnlist;
        })


    ).done(
    $("#jqGrid").jqGrid({
        url: baseURL + 'bss/ddeviceold/list?dpGuid='+GetUrlParam("dpGuid")+'&pGuid='+GetUrlParam("pGuid"),
        datatype: "json",
        colModel: [
            { label: 'guid', name: 'guid', index: 'guid', width: 50, key: true,hidden:true },
            {hidden:true, label: '所属项目ID', name: 'pGuid', index: 'p_guid', width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    for (d in allBase) {
                        if (cellvalue == allBase[d].guid) return allBase[d].pBName;
                    }
                }
            },
            { hidden:true,label: '安装位置', name: 'pPGuid', index: 'p_p_guid', width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    for (d in allPro) {
                        if (cellvalue == allPro[d].guid) return allPro[d].pPCode;
                    }
                }
            },
            {hidden:true,  label: '关联设备信息库ID', name: 'dDGuid', index: 'd_d_guid', width: 80 },
            { label: '工作类型', name: 'dRWork', index: 'd_r_work', width: 80 },
            { hidden:true, label: '设备大类', name: 'dRBigtype', index: 'd_r_bigtype', width: 80 },
            { label: '设备类型', name: 'dRMidtype', index: 'd_r_midtype', width: 80 },
            {  label: '设备标签', name: 'dLabel', index: 'd_label', width: 80 },
            { label: '设备名称', name: 'dRName', index: 'd_r_name', width: 80 },
            { label: '主要参数', name: 'dRParameter', index: 'd_r_parameter', width: 80 },
            { label: '电压等级   数据字典 voltage_type(10kV,380V,220V)', name: 'dRVoltage', index: 'd_r_voltage', width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    for (d in voltage_type) {
                        if (cellvalue == voltage_type[d].dKey) return voltage_type[d].dValue;
                    }
                }
            },
            { label: '设备厂家', name: 'dRFactory', index: 'd_r_factory', width: 80 },
            {hidden:true, label: '出厂日期', name: 'dRFactorydate', index: 'd_r_factorydate', width: 80,
                formatter:function(value,options,row){
                    return value!=null?new Date(value).Format('yyyy-MM-dd'):"";
                }
            },
            {hidden:true, label: '投运日期', name: 'dRDeliverydate', index: 'd_r_deliverydate', width: 80,
                formatter:function(value,options,row){
                    return value!=null?new Date(value).Format('yyyy-MM-dd'):"";
                }
            },
            { label: '固定资产卡片编号', name: 'dRCode', index: 'd_r_code', width: 80 },
            { hidden:true,label: '资产原值（万元）', name: 'dRPrice', index: 'd_r_price', width: 80 },
            { label: '处置意见  数据字典 opinion_type', name: 'dROpinion', index: 'd_r_opinion', width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    for (d in opinion_type) {
                        if (cellvalue == opinion_type[d].dKey) return opinion_type[d].dValue;
                    }
                }
            },
            { label: '拟拆除日期', name: 'dRRemovedate', index: 'd_r_removedate', width: 80,
                formatter:function(value,options,row){
                    return value!=null?new Date(value).Format('yyyy-MM-dd'):"";
                }
            },
            { hidden:true,label: ' 处置去向 数据字典where_type', name: 'dRWhere', index: 'd_r_where', width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    for (d in where_type) {
                        if (cellvalue == where_type[d].dKey) return where_type[d].dValue;
                    }
                }
            },
            {hidden:true, label: '资产净值（万元）', name: 'dRNetassets', index: 'd_r_netassets', width: 80 },
            { hidden:true,label: '资产净值（万元）', name: 'dRRate', index: 'd_r_rate', width: 80 },
            {hidden:true, label: '改造内容', name: 'dRReformcontent', index: 'd_r_reformcontent', width: 80 },
            { hidden:true,label: '技改费用', name: 'dRReformcost', index: 'd_r_reformcost', width: 80 },
            { hidden:true,label: '改造后参数', name: 'dRReformparameter', index: 'd_r_reformparameter', width: 80 },
            { hidden:true,label: '购置设备表guid', name: 'dRGuid', index: 'd_r_guid', width: 80 },
            { hidden:true,label: '数据状态', name: 'dRDatastate', index: 'd_r_datastate', width: 80 }

        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    })
	)
});


function getdatadictionary(sss)
{

    $.get(baseURL + "api/bdatadictionary/select?d_code="+sss, function(r){
        if(sss=="voltage_type") {
            voltage_type=r.returnlist;
        }
        if(sss=="opinion_type") {
            opinion_type=r.returnlist;
        }
        if(sss=="where_type") {
            where_type=r.returnlist;
        }

        vm.voltage_type=voltage_type;
        vm.opinion_type=opinion_type;
        vm.where_type=where_type;
    });
}


var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
        q:{
            qLabel:null,
            qName:null
        },

		dDeviceOld: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.dDeviceOld = {};
		},
		update: function (event) {
			var guid = getSelectedRow();
			if(guid == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.voltage_type=voltage_type;
            vm.opinion_type=opinion_type;
            vm.where_type=where_type;
            vm.getInfo(guid)
		},
		saveOrUpdate: function (event) {
			var url = vm.dDeviceOld.guid == null ? "bss/ddeviceold/save" : "bss/ddeviceold/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.dDeviceOld),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var guids = getSelectedRows();
			if(guids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "bss/ddeviceold/delete",
                    contentType: "application/json",
				    data: JSON.stringify(guids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(guid){
			$.get(baseURL + "bss/ddeviceold/info/"+guid, function(r){
                vm.dDeviceOld = r.dDeviceOld;
                vm.dDeviceOld.dRFactorydate = r.dDeviceOld.dRFactorydate!=null?new Date().Format("yyyy-MM-dd"):"";
                vm.dDeviceOld.dRDeliverydate = r.dDeviceOld.dRDeliverydate!=null?new Date().Format("yyyy-MM-dd"):"";
                vm.dDeviceOld.dRRemovedate = r.dDeviceOld.dRRemovedate!=null?new Date().Format("yyyy-MM-dd"):"";
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'qLabel': vm.q.qLabel,
                    'qName': vm.q.qName,
                },
                page:page
            }).trigger("reloadGrid");
		}
	}
});